Skip to content

fix: define help_msg in main() so --help works from the textfsm script - #153

Merged
harro merged 1 commit into
google:masterfrom
0z5a:fix/cli-help-name-error
Nov 16, 2026
Merged

harro merged 1 commit into
google:masterfrom
0z5a:fix/cli-help-name-error

Conversation

@0z5a

@0z5a 0z5a commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

help_msg is only assigned under if __name__ == '__main__':. The textfsm console script installed by setup.py (textfsm=textfsm.parser:main) calls main() directly, so that block never runs and --help fails:

$ textfsm --help
Traceback (most recent call last):
  ...
  File ".../textfsm/parser.py", line 1096, in main
    print(help_msg)
          ^^^^^^^^
NameError: name 'help_msg' is not defined

python -m textfsm.parser --help works because it goes through the __main__ block. textfsm.terminal.main() has the same pattern and fails the same way when it is called directly.

This change builds the usage line inside main() from argv[0] (which is sys.argv unless the caller passes its own list) and drops the module-level assignment from the __main__ blocks. The text of the usage line is unchanged.

Tests: a UnitTestMain.testHelp case in tests/textfsm_test.py and in tests/terminal_test.py calls main([..., '--help']) and checks the return value and the usage line. Both fail with the NameError above without the change and pass with it. pytest tests: 110 passed. With the change, the installed textfsm --help prints the docstring and usage line and exits 0.

This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.

help_msg was only set under `if __name__ == '__main__':`, but the
installed `textfsm` console script calls textfsm.parser:main()
directly, so `textfsm --help` failed with
"NameError: name 'help_msg' is not defined". terminal.main() had the
same problem when called directly.

Build the usage line inside main() from argv[0], and add tests that
call main() with --help.
@harro
harro merged commit 40cacbf into google:master Nov 16, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants